<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formPageEventTrait{
    public function index_pageevent(Content $content)
    {
        return $content->title('页面自动事件')->description('进入某个页面后，自动执行的一些动作，比如进入首页弹窗就在这里配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_pageevent()->render()}</div>
HTML
            );
    }
    protected function form_pageevent(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->table('app_page_event_list','自动事件', function (NestedForm $table) {
            $table->radio('scene', '页面')->options()->required()->width(5);
            $table->text('scene_id', '页面id');
            $table->radio('type', '类型')->options(FieldUtils::getUrlTypes())->default(0)->width(2);
            $table->text('target', '跳转id')->help('<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            $table->number('interval', '触发间隔(单位/分钟)')->default(999999999999999);
        })->default(Settings::get('app_page_event_list', ''));

        $form->action('settings/action/pageevent');
        return $form;
    }

    public function pageevent(Request $request){
        SiteUtils::forget_config_data();

        Settings::set('app_page_event_list', AdminUtils::_table_data($request->input('app_page_event_list')), true);


        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);

        return $this->response()->success('成功！');
    }

}
